package com.lizard.schedule.utils;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lizard.schedule.R;

/**
 * 状态栏相关工具类
 */
public class StatusBarUtil {

    /**
     * 设置状态栏颜色 android 4.4之后版本生效
     * 用法：在activity的onCreate调用即可
     *
     * @param activity
     * @param colorResId
     */
    public static void setStatusBarColor(Activity activity, int colorResId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            // 状态栏透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            ViewGroup decorViewGroup = (ViewGroup) activity.getWindow().getDecorView();

            // 状态栏view
            FrameLayout statusBar = new FrameLayout(activity);
            statusBar.setId(R.id.status_bar);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtil.getStatusHeight(activity));
            statusBar.setLayoutParams(params);
            statusBar.setBackgroundColor(activity.getResources().getColor(colorResId));

            // 新的child view
            LinearLayout newChild = new LinearLayout(activity);
            LinearLayout.LayoutParams newParentParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            newChild.setOrientation(LinearLayout.VERTICAL);

            // 旧的child移除
            View oldChildView = decorViewGroup.getChildAt(0);
            decorViewGroup.removeView(oldChildView);

            // 新的child里面加入状态条和旧的child
            newChild.addView(statusBar);
            newChild.addView(oldChildView);

            // 将新的child加入页面
            decorViewGroup.addView(newChild);
        }
    }

}
